<?php

/**
 * Query custom get post 
 */
function agilsun_query($catogery, $numpost) {
    query_posts(array('category_name' => $catogery, // choose catogery which u want to get
        'posts_per_page' => $numpost, //max num post was got      
                )
    );
}

/**
 * Query get all post with cat slug
 */
function agilsun_query_get_all_post($catslug, $numpost) {
    query_posts(array('category_name' => $catslug,
        'posts_per_page' => $numpost));
}

/**
 * Agilsun get post top content in home
 */
function agilsun_get_post_top_content($catogery, $numpost) {
    agilsun_query($catogery, $numpost);
    echo "<ul class='post-content group row'>";
    while (have_posts()) : the_post();
        ?>
        <li class="col-md-3">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail(); ?>
                <p><?php echo the_title(); ?></p>
            </a>
        </li>
        <?php
    endwhile;
    echo "</ul>";
    wp_reset_query();
}

/**
 * Agilsun get post news slide
 */
function agilsun_get_post_news_slide($catogery, $numpost) {
    agilsun_query_get_all_post($catogery, $numpost);
    ?><ul class="slides"><?php
    while (have_posts()) : the_post();
        ?>      <li class="m-thumb entry-image hover-content"><a href="<?php the_permalink(); ?>">     
                    <?php the_post_thumbnail('msize'); ?></a>
                <p><?php echo the_title(); ?></p>
            </li>
            <?php
        endwhile;
        wp_reset_query();
        ?></ul><?php
}

/**
 * excerpt de hien thi link continue reading
 */
function new_excerpt_length($length) {
    return 50;
}

add_filter('excerpt_length', 'new_excerpt_length');

/**
 * doi dau ... thanh Xem Hết
 */
function new_excerpt_more($more) {
    global $post;
    return "...";
}

add_filter('excerpt_more', 'new_excerpt_more');

/**
 * lấy tất cả post của category hiện tại
 */
function agilsun_get_post_cat() {
    while (have_posts()) : the_post();
        ?>
        <li class="news-body-content-post">
            <?php the_post_thumbnail('nsize'); ?>
            <a href="<?php the_permalink(); ?>">
                <h2><?php the_title(); ?></h2>
            </a>
            <?php the_time('M d, Y'); ?><span>|</span><?php comments_popup_link('0 bình luận', '1 bình luận', '% bình luận'); ?>
            <p>
                <?php echo get_the_excerpt(); ?>
            </p>
            <a class='read_more' href='<?php the_permalink(); ?>'>xem thêm</a>
        </li>
        <?php
    endwhile;
    wp_reset_query();
}

/**
 * lấy tên category hiện tại
 */
function agilsun_get_current_cat_name() {
    if (is_category()) {
        $cat = get_query_var('cat');
        $yourcat = get_category($cat);
        agilsun_get_catogery_name($yourcat->slug);
    }
}

/**
 * lấy post của category bằng slug
 */
function agilsun_get_post_cat_by_slug($slug, $numpost) {
    agilsun_query_get_all_post($slug, $numpost);
    while (have_posts()) : the_post();
        ?>
        <li>
            <div class="row">
                <a href="<?php the_permalink(); ?>" class="col-md-4"><?php the_post_thumbnail('msize'); ?></a>
                <div class="col-md-8">
                    <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?> </h2></a>
                    <span>Ngày: <?php the_time('d/m/Y'); ?></span>
                    <p> <?php echo get_the_excerpt(); ?></p>                         
                </div>
            </div>
        </li>
        <?php
    endwhile;
    wp_reset_query();
}

/**
 * Get catagory name by slug
 */
function agilsun_get_catogery_name($slug) {
    $catt = get_category_by_slug($slug);
    echo $catt->name;
}

/**
 * Get permalink catagory by slug
 */
function agilsun_get_permalink_catogery($slug) {
    $catt = get_category_by_slug($slug);
    echo get_category_link($catt->term_id);
}

/**
 * Get permalink page by slug
 */
function agilsun_get_permalink_page($page_title) {
    $page = get_page_by_title($page_title);
    echo get_page_link($page->ID);
}

add_post_type_support('page', 'excerpt');
?>  